package com.nhncorp.pleiades.master;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.transaction.locking.ReadWriteLockManager;
import org.apache.commons.transaction.util.CommonsLoggingLogger;
import org.apache.commons.transaction.util.LoggerFacade;

public class FileTransactionManager {
  
  private static final Log log = LogFactory.getLog(FileTransactionManager.class.getName());
  private static final LoggerFacade sLogger = new CommonsLoggingLogger(log);
  private ReadWriteLockManager lockManager = null;
  
  public FileTransactionManager() {
    lockManager = new ReadWriteLockManager(sLogger, 2000);
  }
  
  public void writeLock(Object txId, String resourceId) {
    try {
      log.debug(txId + " writeLock " + resourceId);
      lockManager.writeLock(txId, resourceId);
    } catch (Exception e) {
      e.printStackTrace();
    }

  }
  
  public void readLock(Object txId, String resourceId) {
    try {
      log.debug(txId + " readLockLock " + resourceId);
      lockManager.readLock(txId, resourceId);
    } catch(Exception e ) {
      e.printStackTrace();
    }
  }
  
  
  public void releaseLock(Object txId, String resourceId) {
    try {
      log.debug(txId + " releaseLock " + resourceId);
      lockManager.release(txId, resourceId);
    } catch(Exception e ) {
      e.printStackTrace();
    }
  }
  
  public void releaseAll(Object txId) {
    try {
      log.debug(txId + " releaseAll");
      lockManager.releaseAll(txId);
    } catch(Exception e ) {
      e.printStackTrace();
    }
  }  

}
